package translinkApp.DataTypes.GoCard;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import translinkApp.Data.GoCard.GoCardTransaction;
import translinkApp.Data.Utils.Functions;

public class GoCardInfo {

	private String issueDate;
	private String expiryDate;
	private String balance;
	private String passengerType;
	private String balanceTime;

	public String getBalanceTime() {
		return balanceTime;
	}

	public String getIssueDate() {
		return issueDate;
	}

	public String getExpiryDate() {
		return expiryDate;
	}

	public String getBalance() {
		return balance;
	}

	public String getPassengerType() {
		return passengerType;
	}

	public GoCardInfo(String cardNum, String password) throws GoCardInfoBadReadException,
			GoCardInfoLoginException, GoCardInfoConnectionException {
		String url = String
				.format("https://www.seqits.com.au/webtix/welcome/welcome.do?cardOps=Display&cardNum=%s&pass=%s",
						cardNum, password);
		HttpClient httpclient = new DefaultHttpClient();

		HttpGet httpget = new HttpGet(url);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		try {
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (Exception e1) {
			throw new GoCardInfoConnectionException();
		}

		if (responseBody.contains("Unable to retrieve")) {
			throw new GoCardInfoLoginException();

		}

		String issueRegex = "Issued Date:</td>.*?<td.*?>(.*?)</td>";
		String expireRegex = "Expiry Date:</td>.*?<td.*?>(.*?)</td>";
		String balanceRegex = "Card Balance</th>.*?<td>.*?<td>(.*?)</td>";
		String balanceTimeRegex = "Card Balance</th>.*?<td>(.*?)</td>";
		String passengerTypeRegex = "Passenger Type:</td>.*?<td.*?>(.*?)</td>";

		try {
			String[] issueDates = Functions.GetRegex(issueRegex, responseBody, true);
			String[] expireDates = Functions.GetRegex(expireRegex, responseBody, true);
			String[] balances = Functions.GetRegex(balanceRegex, responseBody, true);
			String[] balanceTimes = Functions.GetRegex(balanceTimeRegex, responseBody, true);
			String[] passengerTypes = Functions.GetRegex(passengerTypeRegex, responseBody, true);

			issueDate = issueDates[0].trim();
			expiryDate = expireDates[0].trim();
			balance = balances[0];
			balanceTime = balanceTimes[0];
			passengerType = passengerTypes[0];
		} catch (Exception e) {
			throw new GoCardInfoBadReadException(responseBody);
		}

		httpclient.getConnectionManager().shutdown();
	}

	public static final ArrayList<GoCardTransaction> getGoCardTransactions(String cardNum,
			String password, String startDate, String endDate) throws ClientProtocolException,
			IOException {

		String url = String
				.format("https://www.seqits.com.au/webtix/welcome/welcome.do?cardOps=Display&cardNum=%s&pass=%s",
						cardNum, password);
		HttpClient httpclient = new DefaultHttpClient();

		HttpGet httpget = new HttpGet(url);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = httpclient.execute(httpget, responseHandler);

		url = String
				.format("https://www.seqits.com.au/webtix/cardinfo/history.do?cardOps=display&cardNum=%s&pass=%s&startDate=%s&endDate=%s&Refresh=Refresh",
						cardNum, password, startDate, endDate);

		ArrayList<GoCardTransaction> alTemp = new ArrayList<GoCardTransaction>();

		httpget = new HttpGet(url);
		responseHandler = new BasicResponseHandler();
		responseBody = httpclient.execute(httpget, responseHandler);

		String resultsTableRegex = "results_table.*?(.*?)</table>";
		String resultsRowsRegex = "<tr.*?>(.*?)</tr>";
		String resultsColumnsRegex = "<td.*?>(.*?)</td>";
		String resultsColumnHeaderRegex = "<th.*?>(.*?)</th>";

		String[] table = Functions.GetRegex(resultsTableRegex, responseBody, true);

		String[] rows = Functions.GetRegex(resultsRowsRegex, table[0], true);
		String[] columns;

		for (String r : rows) {
			if (r.contains("</th>")) {
				columns = Functions.GetRegex(resultsColumnHeaderRegex, r, true);
			} else {
				columns = Functions.GetRegex(resultsColumnsRegex, r, true);
			}

			if (columns.length >= 4) {

				String time = columns[0].replace("\r\n", "").trim();
				if (time.equals("Date/Time")) {
					continue;
				}

				String location = columns[2].replace("\r\n", "").replace("&nbsp;", "").trim();
				location = URLDecoder.decode(location, "UTF-8");
				location = location.replace("&#039;", "'");

				alTemp.add(new GoCardTransaction(time, columns[1].replace("\r\n", "").trim(),
						location, columns[3].replace("\r\n", "").replace("&nbsp;", "").trim()));
			}
		}

		return alTemp;

	}

}